jQuery(function($){
  $("a.btn.btn-primary.active").on("click", function(){
    BootstrapDialog.show({
            title: '搜索好友',
            message: "<p class='text-warning'>*注: 为保证公司隐私，名称搜索只支持全称.</p>"
                    + "搜索好友: <input type='text' class='js-search-store-friend' placeholder= '搜索公司名称或者系统id!'>",
            buttons: [{
                label: '取消',
                action: function(dialog) {
                    dialog.close();
                }
            }, {
                label: '确定',
                cssClass: 'btn-primary js-to-search-store-friend',
                action: function(dialog){
                    var searchValue = $("input.js-search-store-friend").val();
                    var q = {};
                    if (searchValue == "" || searchValue == null){
                      q['name_or_uid_eq'] = 0
                      BootstrapDialog.alert('请输入要查询的公司!');
                      return false
                    }else {
                      q['name_or_uid_eq'] = searchValue
                    }
                    $.ajax({
                      url: '/api/stores',
                      method: 'get',
                      data: {q: q},
                      success: function(data){
                        if(data.status == true){
                          dialog.setMessage(
                            "<p class='text-warning'>*注: 为保证公司隐私，名称搜索只支持全称.</p>"
                           +"搜索好友: <input type='text' value="+ searchValue
                           +" class='js-search-store-friend' placeholder= '搜索公司名称或者系统id!'></br></br>"
                           +"<span>公司名称: "+ data.name +"</span></br>"
                           +"<span>法人代码: "+ data.legal_person + "</span></br>"
                           +"系统ID: "+ data.uid + "</br></br>"
                           +"请输入验证信息:</br>"
                           +"<textarea class='form-control js-to-add-store-validation' rows='3'></textarea>"
                           +"<p><button class='btn btn-mini btn-primary js-to-add-store-friend' data-id="+ data.id +" data-name="+ data.name +" data-uid="+ data.uid +" data-base-id="+ data.base_id +" type='button'><i class='fa fa-user-plus'></i>添加好友</button></p>"
                         )
                        }else {
                          dialog.setMessage(
                            "<p class='text-warning'>*注: 为保证公司隐私，名称搜索只支持全称.</p>"
                           +"搜索好友: <input type='text' value="+ searchValue
                           +" class='js-search-store-friend' placeholder= '搜索公司名称或者系统id!'></br></br>"

                           +"不存在该友好公司"
                          )
                        }
                      }
                    }) //ajax end
                }
            }]
        });
  }) //click search store end

  $(document).on("click", "button.js-to-add-store-friend", function(){
    var validation = $("textarea.js-to-add-store-validation").val();
    var friendId = $(this).data("id");
    var baseId = $(this).data("base-id");
    var name = $(this).data("name");
    var uid = $(this).data("uid");
    var csrf = $("input#js-ruiqu-store-friend-index").val();

    $.ajax({
      url: "/api/store_friends/friended",
      method: "get",
      data: {friend_id: friendId},
      success: function(data){
        if (data.status == true) {
          BootstrapDialog.alert("已经是好友关系!");
        }else {
          $.ajax({
            url: "/api/store_friends",
            method: "post",
            headers: {
                      'X-CSRF-Token': csrf
                    },
            data: {
                    friend_id: friendId,
                    validation: validation,
                    base_id: baseId,
                    name: name,
                    uid: uid
                  },
            success: function(data){

            }
          }) //second ajax

        }
      }
    })


  })

})
